# Decompiled with PyLingual (https://pylingual.io)
# Internal filename: Mitest.py
# Bytecode version: 3.11a7e (3495)
# Source timestamp: 1970-01-01 00:00:00 UTC (0)

import serial
import time
import argparse

def send_serial_command(port, hex_command):
    # 定义要发送的十六进制指令
    try:
        #将十六进制字符转换为字节数据
        byte_data = bytes.fromhex(hex_command)
    except ValueError as e:
        print(f'指令格式错误: {e}')
        return None
    try:
        # 初始化串口连接
        with serial.Serial(port, baudrate=115200, timeout=5) as ser:
            print(f'已连接到串口: {port}')
            # 发送指令
            ser.write(byte_data)
            print(f'已发送指令: {hex_command}')
            # 可选：读取响应（根据设备协调）
    except serial.SerialException as e:
            print(f'串口连接失败: {e}')
        except Exception as e:
            print(f'发生错误: {e}')
if __name__ == '__main__':
    # 命令行参数解析
    port = input('请输入MCU串口号(如COM3):').strip()
    cycle = input('请输入测试间隔(单位:S):')
    parser = argparse.ArgumentParser(description='串口指令发送工具')
    hex_command = '0F DE 00 81 00 09 00 31 31 34 37 32 35 38 23 01 04'  #密码值147258
    reset = '0F DE 00 81 00 02 02 01 01 04'  #长按重置
    test = '0F DE 00 81 00 02 02 00 01 04'   #长按设置
    log = '0F DE 00 87 00 01 05 01 04'       #log锁定
    for i in range(1, 10000):
        try:
            print(f'Mitest版本自动化测试，第' + str(i) + '次测试')
            send_serial_command(port, hex_command)
            time.sleep(int(cycle))
        except Exception as e:
            print('程序异常')